home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / show / mpeg2decodeWOS.lha / mpeg2decode / src / getpic.c < prev    next >
C/C++ Source or Header  |  1999-02-23  |  36KB  |  1,291 lines

  1. /* getpic.c, picture decoding                                               */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "config.h"
  33. #include "global.h"
  34.  
  35. /* private prototypes*/
  36. static void picture_data _ANSI_ARGS_((int framenum));
  37. static void macroblock_modes _ANSI_ARGS_((int *pmacroblock_type, int *pstwtype,
  38.   int *pstwclass, int *pmotion_type, int *pmotion_vector_count, int *pmv_format, int *pdmv,
  39.   int *pmvscale, int *pdct_type));
  40. static void Clear_Block _ANSI_ARGS_((int comp));
  41. static void Sum_Block _ANSI_ARGS_((int comp));
  42. static void Saturate _ANSI_ARGS_((short *bp));
  43. static void Add_Block _ANSI_ARGS_((int comp, int bx, int by,
  44.   int dct_type, int addflag));
  45. static void Update_Picture_Buffers _ANSI_ARGS_((void));
  46. static void frame_reorder _ANSI_ARGS_((int bitstream_framenum, 
  47.   int sequence_framenum));
  48. static void Decode_SNR_Macroblock _ANSI_ARGS_((int *SNRMBA, int *SNRMBAinc, 
  49.   int MBA, int MBAmax, int *dct_type));
  50.  
  51. static void motion_compensation _ANSI_ARGS_((int MBA, int macroblock_type, 
  52.  int motion_type, int PMV[2][2][2], int motion_vertical_field_select[2][2], 
  53.  int dmvector[2], int stwtype, int dct_type));
  54.  
  55. static void skipped_macroblock _ANSI_ARGS_((int dc_dct_pred[3], 
  56.   int PMV[2][2][2], int *motion_type, int motion_vertical_field_select[2][2],
  57.   int *stwtype, int *macroblock_type));
  58.  
  59. static int slice _ANSI_ARGS_((int framenum, int MBAmax));
  60.  
  61. static int start_of_slice _ANSI_ARGS_ ((int MBAmax, int *MBA,
  62.   int *MBAinc, int dc_dct_pred[3], int PMV[2][2][2]));
  63.  
  64. static int decode_macroblock _ANSI_ARGS_((int *macroblock_type, 
  65.   int *stwtype, int *stwclass, int *motion_type, int *dct_type,
  66.   int PMV[2][2][2], int dc_dct_pred[3], 
  67.   int motion_vertical_field_select[2][2], int dmvector[2]));
  68.  
  69.  
  70. /* decode one frame or field picture */
  71. #ifdef STORM
  72. void Decode_Picture(int bitstream_framenum, int sequence_framenum)
  73. #else
  74. void Decode_Picture(bitstream_framenum, sequence_framenum)
  75. int bitstream_framenum, sequence_framenum;
  76. #endif
  77. {
  78.  
  79.   if (picture_structure==FRAME_PICTURE && Second_Field)
  80.   {
  81.     /* recover from illegal number of field pictures */
  82.     printf("odd number of field pictures\n");
  83.     Second_Field = 0;
  84.   }
  85.  
  86.   /* IMPLEMENTATION: update picture buffer pointers */
  87.   Update_Picture_Buffers();
  88.  
  89. #ifdef VERIFY 
  90.   Check_Headers(bitstream_framenum, sequence_framenum);
  91. #endif /* VERIFY */
  92.  
  93.   /* ISO/IEC 13818-4 section 2.4.5.4 "frame buffer intercept method" */
  94.   /* (section number based on November 1995 (Dallas) draft of the 
  95.       conformance document) */
  96.   if(Ersatz_Flag)
  97.     Substitute_Frame_Buffer(bitstream_framenum, sequence_framenum);
  98.  
  99.   /* form spatial scalable picture */
  100.  
  101.   /* form spatial scalable picture */
  102.   /* ISO/IEC 13818-2 section 7.7: Spatial scalability */
  103.   if (base.pict_scal && !Second_Field) 
  104.   {
  105.     Spatial_Prediction();
  106.   }
  107.  
  108.   /* decode picture data ISO/IEC 13818-2 section 6.2.3.7 */
  109.   picture_data(bitstream_framenum);
  110.  
  111.   /* write or display current or previously decoded reference frame */
  112.   /* ISO/IEC 13818-2 section 6.1.1.11: Frame reordering */
  113.   frame_reorder(bitstream_framenum, sequence_framenum);
  114.  
  115.   if (picture_structure!=FRAME_PICTURE)
  116.     Second_Field = !Second_Field;
  117. }
  118.  
  119.  
  120. /* decode all macroblocks of the current picture */
  121. /* stages described in ISO/IEC 13818-2 section 7 */
  122. #ifdef STORM
  123. static void picture_data(int framenum)
  124. #else
  125. static void picture_data(framenum)
  126. int framenum;
  127. #endif
  128. {
  129.   int MBAmax;
  130.   int ret;
  131.  
  132.   /* number of macroblocks per picture */
  133.   MBAmax = mb_width*mb_height;
  134.  
  135.   if (picture_structure!=FRAME_PICTURE)
  136.     MBAmax>>=1; /* field picture has half as mnay macroblocks as frame */
  137.  
  138.   for(;;)
  139.   {
  140.     if((ret=slice(framenum, MBAmax))<0)
  141.       return;
  142.   }
  143.  
  144. }
  145.  
  146.  
  147.  
  148. /* decode all macroblocks of the current picture */
  149. /* ISO/IEC 13818-2 section 6.3.16 */
  150. #ifdef STORM
  151. static int slice(int framenum, int MBAmax)
  152. #else
  153. static int slice(framenum, MBAmax)
  154. int framenum, MBAmax;
  155. #endif
  156. {
  157.   int MBA; 
  158.   int MBAinc, macroblock_type, motion_type, dct_type;
  159.   int dc_dct_pred[3];
  160.   int PMV[2][2][2], motion_vertical_field_select[2][2];
  161.   int dmvector[2];
  162.   int stwtype, stwclass;
  163.   int SNRMBA, SNRMBAinc;
  164.   int ret;
  165.  
  166.   MBA = 0; /* macroblock address */
  167.   MBAinc = 0;
  168.  
  169.   if((ret=start_of_slice(MBAmax, &MBA, &MBAinc, dc_dct_pred, PMV))!=1)
  170.     return(ret);
  171.  
  172.   if (Two_Streams && enhan.scalable_mode==SC_SNR)
  173.   {
  174.     SNRMBA=0;
  175.     SNRMBAinc=0;
  176.   }
  177.  
  178.   Fault_Flag=0;
  179.  
  180.   for (;;)
  181.   {
  182.  
  183.     /* this is how we properly exit out of picture */
  184.     if (MBA>=MBAmax)
  185.       return(-1); /* all macroblocks decoded */
  186.  
  187. #ifdef TRACE
  188.     if (Trace_Flag)
  189.       printf("frame %d, MB %d\n",framenum,MBA);
  190. #endif /* TRACE */
  191.  
  192. #ifdef DISPLAY
  193.     if (!progressive_frame && picture_structure==FRAME_PICTURE 
  194.       && MBA==(MBAmax>>1) && framenum!=0 && Output_Type==T_X11 
  195.        && !Display_Progressive_Flag)
  196.     {
  197.       Display_Second_Field();
  198.     }
  199. #endif
  200.  
  201.     ld = &base;
  202.  
  203.     if (MBAinc==0)
  204.     {
  205.       if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
  206.           ld = &enhan;
  207.  
  208.       if (!Show_Bits(23) || Fault_Flag) /* next_start_code or fault */
  209.       {
  210. resync: /* if Fault_Flag: resynchronize to next next_start_code */
  211.         Fault_Flag = 0;
  212.         return(0);     /* trigger: go to next slice */
  213.       }
  214.       else /* neither next_start_code nor Fault_Flag */
  215.       {
  216.         if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
  217.           ld = &enhan;
  218.  
  219.         /* decode macroblock address increment */
  220.         MBAinc = Get_macroblock_address_increment();
  221.  
  222.         if (Fault_Flag) goto resync;
  223.       }
  224.     }
  225.  
  226.     if (MBA>=MBAmax)
  227.     {
  228.       /* MBAinc points beyond picture dimensions */
  229.       if (!Quiet_Flag)
  230.         printf("Too many macroblocks in picture\n");
  231.       return(-1);
  232.     }
  233.  
  234.     if (MBAinc==1) /* not skipped */
  235.     {
  236.       ret = decode_macroblock(¯oblock_type, &stwtype, &stwclass,
  237.               &motion_type, &dct_type, PMV, dc_dct_pred, 
  238.               motion_vertical_field_select, dmvector);
  239.  
  240.       if(ret==-1)
  241.         return(-1);
  242.    
  243.       if(ret==0)
  244.         goto resync;
  245.  
  246.     }
  247.     else /* MBAinc!=1: skipped macroblock */
  248.     {      
  249.       /* ISO/IEC 13818-2 section 7.6.6 */
  250.       skipped_macroblock(dc_dct_pred, PMV, &motion_type, 
  251.         motion_vertical_field_select, &stwtype, ¯oblock_type);
  252.     }
  253.  
  254.     /* SCALABILITY: SNR */
  255.     /* ISO/IEC 13818-2 section 7.8 */
  256.     /* NOTE: we currently ignore faults encountered in this routine */
  257.     if (Two_Streams && enhan.scalable_mode==SC_SNR)
  258.       Decode_SNR_Macroblock(&SNRMBA, &SNRMBAinc, MBA, MBAmax, &dct_type);
  259.  
  260.     /* ISO/IEC 13818-2 section 7.6 */
  261.     motion_compensation(MBA, macroblock_type, motion_type, PMV, 
  262.       motion_vertical_field_select, dmvector, stwtype, dct_type);
  263.  
  264.  
  265.     /* advance to next macroblock */
  266.     MBA++;
  267.     MBAinc--;
  268.  
  269.     /* SCALABILITY: SNR */
  270.     if (Two_Streams && enhan.scalable_mode==SC_SNR)
  271.     {
  272.       SNRMBA++;
  273.       SNRMBAinc--;
  274.     }
  275.  
  276.     if (MBA>=MBAmax)
  277.       return(-1); /* all macroblocks decoded */
  278.   }
  279. }
  280.  
  281.  
  282. /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
  283. #ifdef STORM
  284. static void macroblock_modes(int *pmacroblock_type,int *pstwtype,int *pstwclass,
  285.   int *pmotion_type,int *pmotion_vector_count,int *pmv_format,int *pdmv,int *pmvscale,int *pdct_type)
  286. #else
  287. static void macroblock_modes(pmacroblock_type,pstwtype,pstwclass,
  288.   pmotion_type,pmotion_vector_count,pmv_format,pdmv,pmvscale,pdct_type)
  289.   int *pmacroblock_type, *pstwtype, *pstwclass;
  290.   int *pmotion_type, *pmotion_vector_count, *pmv_format, *pdmv, *pmvscale;
  291.   int *pdct_type;
  292. #endif
  293. {
  294.   int macroblock_type;
  295.   int stwtype, stwcode, stwclass;
  296.   int motion_type = 0;
  297.   int motion_vector_count, mv_format, dmv, mvscale;
  298.   int dct_type;
  299.   static unsigned char stwc_table[3][4]
  300.     = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
  301.   static unsigned char stwclass_table[9]
  302.     = {0, 1, 2, 1, 1, 2, 3, 3, 4};
  303.  
  304.   /* get macroblock_type */
  305.   macroblock_type = Get_macroblock_type();
  306.  
  307.   if (Fault_Flag) return;
  308.  
  309.   /* get spatial_temporal_weight_code */
  310.   if (macroblock_type & MB_WEIGHT)
  311.   {
  312.     if (spatial_temporal_weight_code_table_index==0)
  313.       stwtype = 4;
  314.     else
  315.     {
  316.       stwcode = Get_Bits(2);
  317. #ifdef TRACE
  318.       if (Trace_Flag)
  319.       {
  320.         printf("spatial_temporal_weight_code (");
  321.         Print_Bits(stwcode,2,2);
  322.         printf("): %d\n",stwcode);
  323.       }
  324. #endif /* TRACE */
  325.       stwtype = stwc_table[spatial_temporal_weight_code_table_index-1][stwcode];
  326.     }
  327.   }
  328.   else
  329.     stwtype = (macroblock_type & MB_CLASS4) ? 8 : 0;
  330.  
  331.   /* SCALABILITY: derive spatial_temporal_weight_class (Table 7-18) */
  332.   stwclass = stwclass_table[stwtype];
  333.  
  334.   /* get frame/field motion type */
  335.   if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
  336.   {
  337.     if (picture_structure==FRAME_PICTURE) /* frame_motion_type */
  338.     {
  339.       motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
  340. #ifdef TRACE
  341.       if (!frame_pred_frame_dct && Trace_Flag)
  342.       {
  343.         printf("frame_motion_type (");
  344.         Print_Bits(motion_type,2,2);
  345.         printf("): %s\n",motion_type==MC_FIELD?"Field":
  346.                          motion_type==MC_FRAME?"Frame":
  347.                          motion_type==MC_DMV?"Dual_Prime":"Invalid");
  348.       }
  349. #endif /* TRACE */
  350.     }
  351.     else /* field_motion_type */
  352.     {
  353.       motion_type = Get_Bits(2);
  354. #ifdef TRACE
  355.       if (Trace_Flag)
  356.       {
  357.         printf("field_motion_type (");
  358.         Print_Bits(motion_type,2,2);
  359.         printf("): %s\n",motion_type==MC_FIELD?"Field":
  360.                          motion_type==MC_16X8?"16x8 MC":
  361.                          motion_type==MC_DMV?"Dual_Prime":"Invalid");
  362.       }
  363. #endif /* TRACE */
  364.     }
  365.   }
  366.   else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
  367.   {
  368.     /* concealment motion vectors */
  369.     motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
  370.   }
  371. #if 0
  372.   else
  373.   {
  374.     printf("maroblock_modes(): unknown macroblock type\n");
  375.     motion_type = -1;
  376.   }
  377. #endif
  378.  
  379.   /* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
  380.   if (picture_structure==FRAME_PICTURE)
  381.   {
  382.     motion_vector_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
  383.     mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
  384.   }
  385.   else
  386.   {
  387.     motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
  388.     mv_format = MV_FIELD;
  389.   }
  390.  
  391.   dmv = (motion_type==MC_DMV); /* dual prime */
  392.  
  393.   /* field mv predictions in frame pictures have to be scaled
  394.    * ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
  395.    * IMPLEMENTATION: mvscale is derived for later use in motion_vectors()
  396.    * it displaces the stage:
  397.    *
  398.    *    if((mv_format=="field")&&(t==1)&&(picture_structure=="Frame picture"))
  399.    *      prediction = PMV[r][s][t] DIV 2;
  400.    */
  401.  
  402.   mvscale = ((mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
  403.  
  404.   /* get dct_type (frame DCT / field DCT) */
  405.   dct_type = (picture_structure==FRAME_PICTURE)
  406.              && (!frame_pred_frame_dct)
  407.              && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))
  408.              ? Get_Bits(1)
  409.              : 0;
  410.  
  411. #ifdef TRACE
  412.   if (Trace_Flag  && (picture_structure==FRAME_PICTURE)
  413.              && (!frame_pred_frame_dct)
  414.              && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)))
  415.     printf("dct_type (%d): %s\n",dct_type,dct_type?"Field":"Frame");
  416. #endif /* TRACE */
  417.  
  418.   /* return values */
  419.   *pmacroblock_type = macroblock_type;
  420.   *pstwtype = stwtype;
  421.   *pstwclass = stwclass;
  422.   *pmotion_type = motion_type;
  423.   *pmotion_vector_count = motion_vector_count;
  424.   *pmv_format = mv_format;
  425.   *pdmv = dmv;
  426.   *pmvscale = mvscale;
  427.   *pdct_type = dct_type;
  428. }
  429.  
  430.  
  431. /* move/add 8x8-Block from block[comp] to backward_reference_frame */
  432. /* copy reconstructed 8x8 block from block[comp] to current_frame[]
  433.  * ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
  434.  * This stage also embodies some of the operations implied by:
  435.  *   - ISO/IEC 13818-2 section 7.6.7: Combining predictions
  436.  *   - ISO/IEC 13818-2 section 6.1.3: Macroblock
  437. */
  438. #ifdef STORM
  439. static void Add_Block(int comp,int bx,int by,int dct_type,int addflag)
  440. #else
  441. static void Add_Block(comp,bx,by,dct_type,addflag)
  442. int comp,bx,by,dct_type,addflag;
  443. #endif
  444. {
  445.   int cc,i, j, iincr;
  446.   unsigned char *rfp;
  447.   short *bp;
  448.  
  449.   
  450.   /* derive color component index */
  451.   /* equivalent to ISO/IEC 13818-2 Table 7-1 */
  452.   cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
  453.  
  454.   if (cc==0)
  455.   {
  456.     /* luminance */
  457.  
  458.     if (picture_structure==FRAME_PICTURE)
  459.       if (dct_type)
  460.       {
  461.         /* field DCT coding */
  462.         rfp = current_frame[0]
  463.               + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
  464.         iincr = (Coded_Picture_Width<<1) - 8;
  465.       }
  466.       else
  467.       {
  468.         /* frame DCT coding */
  469.         rfp = current_frame[0]
  470.               + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  471.         iincr = Coded_Picture_Width - 8;
  472.       }
  473.     else
  474.     {
  475.       /* field picture */
  476.       rfp = current_frame[0]
  477.             + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  478.       iincr = (Coded_Picture_Width<<1) - 8;
  479.     }
  480.   }
  481.   else
  482.   {
  483.     /* chrominance */
  484.  
  485.     /* scale coordinates */
  486.     if (chroma_format!=CHROMA444)
  487.       bx >>= 1;
  488.     if (chroma_format==CHROMA420)
  489.       by >>= 1;
  490.     if (picture_structure==FRAME_PICTURE)
  491.     {
  492.       if (dct_type && (chroma_format!=CHROMA420))
  493.       {
  494.         /* field DCT coding */
  495.         rfp = current_frame[cc]
  496.               + Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);
  497.         iincr = (Chroma_Width<<1) - 8;
  498.       }
  499.       else
  500.       {
  501.         /* frame DCT coding */
  502.         rfp = current_frame[cc]
  503.               + Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);
  504.         iincr = Chroma_Width - 8;
  505.       }
  506.     }
  507.     else
  508.     {
  509.       /* field picture */
  510.       rfp = current_frame[cc]
  511.             + (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);
  512.       iincr = (Chroma_Width<<1) - 8;
  513.     }
  514.   }
  515.  
  516.   bp = ld->block[comp];
  517.  
  518.   if (addflag)
  519.   {
  520.     for (i=0; i<8; i++)
  521.     {
  522.       for (j=0; j<8; j++)
  523.       {
  524.         *rfp = Clip[*bp++ + *rfp];
  525.         rfp++;
  526.       }
  527.  
  528.       rfp+= iincr;
  529.     }
  530.   }
  531.   else
  532.   {
  533.     for (i=0; i<8; i++)
  534.     {
  535.       for (j=0; j<8; j++)
  536.         *rfp++ = Clip[*bp++ + 128];
  537.  
  538.       rfp+= iincr;
  539.     }
  540.   }
  541. }
  542.  
  543.  
  544. /* ISO/IEC 13818-2 section 7.8 */
  545. #ifdef STORM
  546. static void Decode_SNR_Macroblock(int *SNRMBA, int *SNRMBAinc, int MBA, int MBAmax, int *dct_type)
  547. #else
  548. static void Decode_SNR_Macroblock(SNRMBA, SNRMBAinc, MBA, MBAmax, dct_type)
  549.   int *SNRMBA, *SNRMBAinc;
  550.   int MBA, MBAmax;
  551.   int *dct_type;
  552. #endif
  553. {
  554.   int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy; 
  555.   int slice_vert_pos_ext, quantizer_scale_code, comp, code;
  556.  
  557.   ld = &enhan;
  558.  
  559.   if (*SNRMBAinc==0)
  560.   {
  561.     if (!Show_Bits(23)) /* next_start_code */
  562.     {
  563.       next_start_code();
  564.       code = Show_Bits(32);
  565.  
  566.       if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  567.       {
  568.         /* only slice headers are allowed in picture_data */
  569.         if (!Quiet_Flag)
  570.           printf("SNR: Premature end of picture\n");
  571.         return;
  572.       }
  573.  
  574.       Flush_Buffer32();
  575.  
  576.       /* decode slice header (may change quantizer_scale) */
  577.       slice_vert_pos_ext = slice_header();
  578.  
  579.       /* decode macroblock address increment */
  580.       *SNRMBAinc = Get_macroblock_address_increment();
  581.  
  582.       /* set current location */
  583.       *SNRMBA =
  584.         ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;
  585.  
  586.       *SNRMBAinc = 1; /* first macroblock in slice: not skipped */
  587.     }
  588.     else /* not next_start_code */
  589.     {
  590.       if (*SNRMBA>=MBAmax)
  591.       {
  592.         if (!Quiet_Flag)
  593.           printf("Too many macroblocks in picture\n");
  594.         return;
  595.       }
  596.  
  597.       /* decode macroblock address increment */
  598.       *SNRMBAinc = Get_macroblock_address_increment();
  599.     }
  600.   }
  601.  
  602.   if (*SNRMBA!=MBA)
  603.   {
  604.     /* streams out of sync */
  605.     if (!Quiet_Flag)
  606.       printf("Cant't synchronize streams\n");
  607.     return;
  608.   }
  609.  
  610.   if (*SNRMBAinc==1) /* not skipped */
  611.   {
  612.     macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,
  613.       &dummy, &dummy, &dummy, &dummy, &dummy,
  614.       &SNRdct_type);
  615.  
  616.     if (SNRmacroblock_type & MACROBLOCK_PATTERN)
  617.       *dct_type = SNRdct_type;
  618.  
  619.     if (SNRmacroblock_type & MACROBLOCK_QUANT)
  620.     {
  621.       quantizer_scale_code = Get_Bits(5);
  622.       ld->quantizer_scale =
  623.         ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
  624.     }
  625.  
  626.     /* macroblock_pattern */
  627.     if (SNRmacroblock_type & MACROBLOCK_PATTERN)
  628.     {
  629.       SNRcoded_block_pattern = Get_coded_block_pattern();
  630.  
  631.       if (chroma_format==CHROMA422)
  632.         SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */
  633.       else if (chroma_format==CHROMA444)
  634.         SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */
  635.     }
  636.     else
  637.       SNRcoded_block_pattern = 0;
  638.  
  639.     /* decode blocks */
  640.     for (comp=0; comp<block_count; comp++)
  641.     {
  642.       Clear_Block(comp);
  643.  
  644.       if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))
  645.         Decode_MPEG2_Non_Intra_Block(comp);
  646.     }
  647.   }
  648.   else /* SNRMBAinc!=1: skipped macroblock */
  649.   {
  650.     for (comp=0; comp<block_count; comp++)
  651.       Clear_Block(comp);
  652.   }
  653.  
  654.   ld = &base;
  655. }
  656.  
  657.  
  658.  
  659. /* IMPLEMENTATION: set scratch pad macroblock to zero */
  660. #ifdef STORM
  661. static void Clear_Block(int comp)
  662. #else
  663. static void Clear_Block(comp)
  664. int comp;
  665. #endif
  666. {
  667.   short *Block_Ptr;
  668.   int i;
  669.  
  670.   Block_Ptr = ld->block[comp];
  671.  
  672.   for (i=0; i<64; i++)
  673.     *Block_Ptr++ = 0;
  674. }
  675.  
  676.  
  677. /* SCALABILITY: add SNR enhancement layer block data to base layer */
  678. /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
  679. #ifdef STORM
  680. static void Sum_Block(int comp)
  681. #else
  682. static void Sum_Block(comp)
  683. int comp;
  684. #endif
  685. {
  686.   short *Block_Ptr1, *Block_Ptr2;
  687.   int i;
  688.  
  689.   Block_Ptr1 = base.block[comp];
  690.   Block_Ptr2 = enhan.block[comp];
  691.  
  692.   for (i=0; i<64; i++)
  693.     *Block_Ptr1++ += *Block_Ptr2++;
  694. }
  695.  
  696.  
  697. /* limit coefficients to -2048..2047 */
  698. /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
  699. #ifdef STORM
  700. static void Saturate(short *Block_Ptr)
  701. #else
  702. static void Saturate(Block_Ptr)
  703. short *Block_Ptr;
  704. #endif
  705. {
  706.   int i, sum, val;
  707.  
  708.   sum = 0;
  709.  
  710.   /* ISO/IEC 13818-2 section 7.4.3: Saturation */
  711.   for (i=0; i<64; i++)
  712.   {
  713.     val = Block_Ptr[i];
  714.  
  715.     if (val>2047)
  716.       val = 2047;
  717.     else if (val<-2048)
  718.       val = -2048;
  719.  
  720.     Block_Ptr[i] = val;
  721.     sum+= val;
  722.   }
  723.  
  724.   /* ISO/IEC 13818-2 section 7.4.4: Mismatch control */
  725.   if ((sum&1)==0)
  726.     Block_Ptr[63]^= 1;
  727.  
  728. }
  729.  
  730.  
  731. /* reuse old picture buffers as soon as they are no longer needed 
  732.    based on life-time axioms of MPEG */
  733. static void Update_Picture_Buffers()
  734. {                           
  735.   int cc;              /* color component index */
  736.   unsigned char *tmp;  /* temporary swap pointer */
  737.  
  738.   for (cc=0; cc<3; cc++)
  739.   {
  740.     /* B pictures do not need to be save for future reference */
  741.     if (picture_coding_type==B_TYPE)
  742.     {
  743.       current_frame[cc] = auxframe[cc];
  744.     }
  745.     else
  746.     {
  747.       /* only update at the beginning of the coded frame */
  748.       if (!Second_Field)
  749.       {
  750.         tmp = forward_reference_frame[cc];
  751.  
  752.         /* the previously decoded reference frame is stored
  753.            coincident with the location where the backward 
  754.            reference frame is stored (backwards prediction is not
  755.            needed in P pictures) */
  756.         forward_reference_frame[cc] = backward_reference_frame[cc];
  757.         
  758.         /* update pointer for potential future B pictures */
  759.         backward_reference_frame[cc] = tmp;
  760.       }
  761.  
  762.       /* can erase over old backward reference frame since it is not used
  763.          in a P picture, and since any subsequent B pictures will use the 
  764.          previously decoded I or P frame as the backward_reference_frame */
  765.       current_frame[cc] = backward_reference_frame[cc];
  766.     }
  767.  
  768.     /* IMPLEMENTATION:
  769.        one-time folding of a line offset into the pointer which stores the
  770.        memory address of the current frame saves offsets and conditional 
  771.        branches throughout the remainder of the picture processing loop */
  772.     if (picture_structure==BOTTOM_FIELD)
  773.       current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;
  774.   }
  775. }
  776.  
  777.  
  778. /* store last frame */
  779.  
  780. #ifdef STORM
  781. void Output_Last_Frame_of_Sequence(int Framenum)
  782. #else
  783. void Output_Last_Frame_of_Sequence(Framenum)
  784. int Framenum;
  785. #endif
  786. {
  787.   if (Second_Field)
  788.     printf("last frame incomplete, not stored\n");
  789.   else
  790.     Write_Frame(backward_reference_frame,Framenum-1);
  791. }
  792.  
  793.  
  794. #ifdef STORM
  795. static void frame_reorder(int Bitstream_Framenum, int Sequence_Framenum)
  796. #else
  797. static void frame_reorder(Bitstream_Framenum, Sequence_Framenum)
  798. int Bitstream_Framenum, Sequence_Framenum;
  799. #endif
  800. {
  801.   /* tracking variables to insure proper output in spatial scalability */
  802.   static int Oldref_progressive_frame, Newref_progressive_frame;
  803.  
  804.   if (Sequence_Framenum!=0)
  805.   {
  806.     if (picture_structure==FRAME_PICTURE || Second_Field)
  807.     {
  808.       if (picture_coding_type==B_TYPE)
  809.         Write_Frame(auxframe,Bitstream_Framenum-1);
  810.       else
  811.       {
  812.         Newref_progressive_frame = progressive_frame;
  813.         progressive_frame = Oldref_progressive_frame;
  814.  
  815.         Write_Frame(forward_reference_frame,Bitstream_Framenum-1);
  816.  
  817.         Oldref_progressive_frame = progressive_frame = Newref_progressive_frame;
  818.       }
  819.     }
  820. #ifdef DISPLAY
  821.     else if (Output_Type==T_X11)
  822.     {
  823.       if(!Display_Progressive_Flag)
  824.         Display_Second_Field();
  825.     }
  826. #endif
  827.   }
  828.   else
  829.     Oldref_progressive_frame = progressive_frame;
  830.  
  831. }
  832.  
  833.  
  834. /* ISO/IEC 13818-2 section 7.6 */
  835. #ifdef STORM
  836. static void motion_compensation(int MBA, int macroblock_type, int motion_type, int PMV[2][2][2],
  837.   int motion_vertical_field_select[2][2], int dmvector[2], int stwtype, int dct_type)
  838. #else
  839. static void motion_compensation(MBA, macroblock_type, motion_type, PMV, 
  840.   motion_vertical_field_select, dmvector, stwtype, dct_type)
  841. int MBA;
  842. int macroblock_type;
  843. int motion_type;
  844. int PMV[2][2][2];
  845. int motion_vertical_field_select[2][2];
  846. int dmvector[2];
  847. int stwtype;
  848. int dct_type;
  849. #endif
  850. {
  851.   int bx, by;
  852.   int comp;
  853.  
  854.   /* derive current macroblock position within picture */
  855.   /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
  856.   bx = 16*(MBA%mb_width);
  857.   by = 16*(MBA/mb_width);
  858.  
  859.   /* motion compensation */
  860.   if (!(macroblock_type & MACROBLOCK_INTRA))
  861.     form_predictions(bx,by,macroblock_type,motion_type,PMV,
  862.       motion_vertical_field_select,dmvector,stwtype);
  863.   
  864.   /* SCALABILITY: Data Partitioning */
  865.   if (base.scalable_mode==SC_DP)
  866.     ld = &base;
  867.  
  868.   /* copy or add block data into picture */
  869.   for (comp=0; comp<block_count; comp++)
  870.   {
  871.     /* SCALABILITY: SNR */
  872.     /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from 
  873.        the two a layers */
  874.     if (Two_Streams && enhan.scalable_mode==SC_SNR)
  875.       Sum_Block(comp); /* add SNR enhancement layer data to base layer */
  876.  
  877.     /* MPEG-2 saturation and mismatch control */
  878.     /* base layer could be MPEG-1 stream, enhancement MPEG-2 SNR */
  879.     /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
  880.     if ((Two_Streams && enhan.scalable_mode==SC_SNR) || ld->MPEG2_Flag)
  881.       Saturate(ld->block[comp]);
  882.  
  883.     /* ISO/IEC 13818-2 section Annex A: inverse DCT */
  884.     if (Reference_IDCT_Flag)
  885.       Reference_IDCT(ld->block[comp]);
  886.     else
  887.       Fast_IDCT(ld->block[comp]);
  888.     
  889.     /* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
  890.     Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
  891.   }
  892.  
  893. }
  894.  
  895.  
  896.  
  897. /* ISO/IEC 13818-2 section 7.6.6 */
  898. #ifdef STORM
  899. static void skipped_macroblock(int dc_dct_pred[3], int PMV[2][2][2], int *motion_type,
  900.   int motion_vertical_field_select[2][2], int *stwtype, int *macroblock_type)
  901. #else
  902. static void skipped_macroblock(dc_dct_pred, PMV, motion_type, 
  903.   motion_vertical_field_select, stwtype, macroblock_type)
  904. int dc_dct_pred[3];
  905. int PMV[2][2][2];
  906. int *motion_type;
  907. int motion_vertical_field_select[2][2];
  908. int *stwtype;
  909. int *macroblock_type;
  910. #endif
  911. {
  912.   int comp;
  913.   
  914.   /* SCALABILITY: Data Paritioning */
  915.   if (base.scalable_mode==SC_DP)
  916.     ld = &base;
  917.  
  918.   for (comp=0; comp<block_count; comp++)
  919.     Clear_Block(comp);
  920.  
  921.   /* reset intra_dc predictors */
  922.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  923.   dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  924.  
  925.   /* reset motion vector predictors */
  926.   /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  927.   if (picture_coding_type==P_TYPE)
  928.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  929.  
  930.   /* derive motion_type */
  931.   if (picture_structure==FRAME_PICTURE)
  932.     *motion_type = MC_FRAME;
  933.   else
  934.   {
  935.     *motion_type = MC_FIELD;
  936.  
  937.     /* predict from field of same parity */
  938.     /* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
  939.        picture */
  940.     motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] = 
  941.       (picture_structure==BOTTOM_FIELD);
  942.   }
  943.  
  944.   /* skipped I are spatial-only predicted, */
  945.   /* skipped P and B are temporal-only predicted */
  946.   /* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
  947.   *stwtype = (picture_coding_type==I_TYPE) ? 8 : 0;
  948.  
  949.  /* IMPLEMENTATION: clear MACROBLOCK_INTRA */
  950.   *macroblock_type&= ~MACROBLOCK_INTRA;
  951.  
  952. }
  953.  
  954.  
  955. /* return==-1 means go to next picture */
  956. /* the expression "start of slice" is used throughout the normative
  957.    body of the MPEG specification */
  958. #ifdef STORM
  959. static int start_of_slice(int MBAmax, int *MBA, int *MBAinc,
  960.   int dc_dct_pred[3], int PMV[2][2][2])
  961. #else
  962. static int start_of_slice(MBAmax, MBA, MBAinc, 
  963.   dc_dct_pred, PMV)
  964. int MBAmax;
  965. int *MBA;
  966. int *MBAinc;
  967. int dc_dct_pred[3];
  968. int PMV[2][2][2];
  969. #endif
  970. {
  971.   unsigned int code;
  972.   int slice_vert_pos_ext;
  973.  
  974.   ld = &base;
  975.  
  976.   Fault_Flag = 0;
  977.  
  978.   next_start_code();
  979.   code = Show_Bits(32);
  980.  
  981.   if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  982.   {
  983.     /* only slice headers are allowed in picture_data */
  984.     if (!Quiet_Flag)
  985.       printf("start_of_slice(): Premature end of picture\n");
  986.  
  987.     return(-1);  /* trigger: go to next picture */
  988.   }
  989.  
  990.   Flush_Buffer32(); 
  991.  
  992.   /* decode slice header (may change quantizer_scale) */
  993.   slice_vert_pos_ext = slice_header();
  994.  
  995.  
  996.   /* SCALABILITY: Data Partitioning */
  997.   if (base.scalable_mode==SC_DP)
  998.   {
  999.     ld = &enhan;
  1000.     next_start_code();
  1001.     code = Show_Bits(32);
  1002.  
  1003.     if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  1004.     {
  1005.       /* only slice headers are allowed in picture_data */
  1006.       if (!Quiet_Flag)
  1007.         printf("DP: Premature end of picture\n");
  1008.       return(-1);    /* trigger: go to next picture */
  1009.     }
  1010.  
  1011.     Flush_Buffer32();
  1012.  
  1013.     /* decode slice header (may change quantizer_scale) */
  1014.     slice_vert_pos_ext = slice_header();
  1015.  
  1016.     if (base.priority_breakpoint!=1)
  1017.       ld = &base;
  1018.   }
  1019.  
  1020.   /* decode macroblock address increment */
  1021.   *MBAinc = Get_macroblock_address_increment();
  1022.  
  1023.   if (Fault_Flag) 
  1024.   {
  1025.     printf("start_of_slice(): MBAinc unsuccessful\n");
  1026.     return(0);   /* trigger: go to next slice */
  1027.   }
  1028.  
  1029.   /* set current location */
  1030.   /* NOTE: the arithmetic used to derive macroblock_address below is
  1031.    *       equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock
  1032.    */
  1033.   *MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
  1034.   *MBAinc = 1; /* first macroblock in slice: not skipped */
  1035.  
  1036.   /* reset all DC coefficient and motion vector predictors */
  1037.   /* reset all DC coefficient and motion vector predictors */
  1038.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  1039.   dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  1040.   
  1041.   /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1042.   PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1043.   PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1044.  
  1045.   /* successfull: trigger decode macroblocks in slice */
  1046.   return(1);
  1047. }
  1048.  
  1049.  
  1050. /* ISO/IEC 13818-2 sections 7.2 through 7.5 */
  1051. #ifdef STORM
  1052. static int decode_macroblock(int *macroblock_type, int *stwtype, int *stwclass,
  1053.   int *motion_type, int *dct_type, int PMV[2][2][2], int dc_dct_pred[3],
  1054.   int motion_vertical_field_select[2][2], int dmvector[2])
  1055. #else
  1056. static int decode_macroblock(macroblock_type, stwtype, stwclass,
  1057.   motion_type, dct_type, PMV, dc_dct_pred, 
  1058.   motion_vertical_field_select, dmvector)
  1059. int *macroblock_type; 
  1060. int *stwtype;
  1061. int *stwclass;
  1062. int *motion_type; 
  1063. int *dct_type;
  1064. int PMV[2][2][2]; 
  1065. int dc_dct_pred[3]; 
  1066. int motion_vertical_field_select[2][2];
  1067. int dmvector[2];
  1068. #endif
  1069. {
  1070.   /* locals */
  1071.   int quantizer_scale_code; 
  1072.   int comp;
  1073.  
  1074.   int motion_vector_count; 
  1075.   int mv_format; 
  1076.   int dmv; 
  1077.   int mvscale;
  1078.   int coded_block_pattern;
  1079.  
  1080.   /* SCALABILITY: Data Patitioning */
  1081.   if (base.scalable_mode==SC_DP)
  1082.   {
  1083.     if (base.priority_breakpoint<=2)
  1084.       ld = &enhan;
  1085.     else
  1086.       ld = &base;
  1087.   }
  1088.  
  1089.   /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
  1090.   macroblock_modes(macroblock_type, stwtype, stwclass,
  1091.     motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,
  1092.     dct_type);
  1093.  
  1094.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1095.  
  1096.   if (*macroblock_type & MACROBLOCK_QUANT)
  1097.   {
  1098.     quantizer_scale_code = Get_Bits(5);
  1099.  
  1100. #ifdef TRACE
  1101.     if (Trace_Flag)
  1102.     {
  1103.       printf("quantiser_scale_code (");
  1104.       Print_Bits(quantizer_scale_code,5,5);
  1105.       printf("): %d\n",quantizer_scale_code);
  1106.     }
  1107. #endif /* TRACE */
  1108.  
  1109.     /* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
  1110.     if (ld->MPEG2_Flag)
  1111.       ld->quantizer_scale =
  1112.       ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] 
  1113.        : (quantizer_scale_code << 1);
  1114.     else
  1115.       ld->quantizer_scale = quantizer_scale_code;
  1116.  
  1117.     /* SCALABILITY: Data Partitioning */
  1118.     if (base.scalable_mode==SC_DP)
  1119.       /* make sure base.quantizer_scale is valid */
  1120.       base.quantizer_scale = ld->quantizer_scale;
  1121.   }
  1122.  
  1123.   /* motion vectors */
  1124.  
  1125.  
  1126.   /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
  1127.  
  1128.   /* decode forward motion vectors */
  1129.   if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD) 
  1130.     || ((*macroblock_type & MACROBLOCK_INTRA) 
  1131.     && concealment_motion_vectors))
  1132.   {
  1133.     if (ld->MPEG2_Flag)
  1134.       motion_vectors(PMV,dmvector,motion_vertical_field_select,
  1135.         0,motion_vector_count,mv_format,f_code[0][0]-1,f_code[0][1]-1,
  1136.         dmv,mvscale);
  1137.     else
  1138.       motion_vector(PMV[0][0],dmvector,
  1139.       forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
  1140.   }
  1141.  
  1142.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1143.  
  1144.   /* decode backward motion vectors */
  1145.   if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
  1146.   {
  1147.     if (ld->MPEG2_Flag)
  1148.       motion_vectors(PMV,dmvector,motion_vertical_field_select,
  1149.         1,motion_vector_count,mv_format,f_code[1][0]-1,f_code[1][1]-1,0,
  1150.         mvscale);
  1151.     else
  1152.       motion_vector(PMV[0][1],dmvector,
  1153.         backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
  1154.   }
  1155.  
  1156.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1157.  
  1158.   if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
  1159.     Flush_Buffer(1); /* remove marker_bit */
  1160.  
  1161.   if (base.scalable_mode==SC_DP && base.priority_breakpoint==3)
  1162.     ld = &enhan;
  1163.  
  1164.   /* macroblock_pattern */
  1165.   /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
  1166.   if (*macroblock_type & MACROBLOCK_PATTERN)
  1167.   {
  1168.     coded_block_pattern = Get_coded_block_pattern();
  1169.  
  1170.     if (chroma_format==CHROMA422)
  1171.     {
  1172.       /* coded_block_pattern_1 */
  1173.       coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2); 
  1174.  
  1175. #ifdef TRACE
  1176.        if (Trace_Flag)
  1177.        {
  1178.          printf("coded_block_pattern_1: ");
  1179.          Print_Bits(coded_block_pattern,2,2);
  1180.          printf(" (%d)\n",coded_block_pattern&3);
  1181.        }
  1182. #endif /* TRACE */
  1183.      }
  1184.      else if (chroma_format==CHROMA444)
  1185.      {
  1186.       /* coded_block_pattern_2 */
  1187.       coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6); 
  1188.  
  1189. #ifdef TRACE
  1190.       if (Trace_Flag)
  1191.       {
  1192.         printf("coded_block_pattern_2: ");
  1193.         Print_Bits(coded_block_pattern,6,6);
  1194.         printf(" (%d)\n",coded_block_pattern&63);
  1195.       }
  1196. #endif /* TRACE */
  1197.     }
  1198.   }
  1199.   else
  1200.     coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ? 
  1201.       (1<<block_count)-1 : 0;
  1202.  
  1203.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1204.  
  1205.   /* decode blocks */
  1206.   for (comp=0; comp<block_count; comp++)
  1207.   {
  1208.     /* SCALABILITY: Data Partitioning */
  1209.     if (base.scalable_mode==SC_DP)
  1210.     ld = &base;
  1211.  
  1212.     Clear_Block(comp);
  1213.  
  1214.     if (coded_block_pattern & (1<<(block_count-1-comp)))
  1215.     {
  1216.       if (*macroblock_type & MACROBLOCK_INTRA)
  1217.       {
  1218.         if (ld->MPEG2_Flag)
  1219.           Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
  1220.         else
  1221.           Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
  1222.       }
  1223.       else
  1224.       {
  1225.         if (ld->MPEG2_Flag)
  1226.           Decode_MPEG2_Non_Intra_Block(comp);
  1227.         else
  1228.           Decode_MPEG1_Non_Intra_Block(comp);
  1229.       }
  1230.  
  1231.       if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1232.     }
  1233.   }
  1234.  
  1235.   if(picture_coding_type==D_TYPE)
  1236.   {
  1237.     /* remove end_of_macroblock (always 1, prevents startcode emulation) */
  1238.     /* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
  1239.     marker_bit("D picture end_of_macroblock bit");
  1240.   }
  1241.  
  1242.   /* reset intra_dc predictors */
  1243.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  1244.   if (!(*macroblock_type & MACROBLOCK_INTRA))
  1245.     dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  1246.  
  1247.   /* reset motion vector predictors */
  1248.   if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
  1249.   {
  1250.     /* intra mb without concealment motion vectors */
  1251.     /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1252.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1253.     PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1254.   }
  1255.  
  1256.   /* special "No_MC" macroblock_type case */
  1257.   /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
  1258.   if ((picture_coding_type==P_TYPE) 
  1259.     && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
  1260.   {
  1261.     /* non-intra mb without forward mv in a P picture */
  1262.     /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1263.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1264.  
  1265.     /* derive motion_type */
  1266.     /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
  1267.     if (picture_structure==FRAME_PICTURE)
  1268.       *motion_type = MC_FRAME;
  1269.     else
  1270.     {
  1271.       *motion_type = MC_FIELD;
  1272.       /* predict from field of same parity */
  1273.       motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
  1274.     }
  1275.   }
  1276.  
  1277.   if (*stwclass==4)
  1278.   {
  1279.     /* purely spatially predicted macroblock */
  1280.     /* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
  1281.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1282.     PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1283.   }
  1284.  
  1285.   /* successfully decoded macroblock */
  1286.   return(1);
  1287.  
  1288. } /* decode_macroblock */
  1289.  
  1290.  
  1291.